#include "widget.h"
#include "ui_widget.h"
#include <QDir>
#include <QDebug>



Widget::Widget(QWidget *parent)
    : baseFrameMove(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);   //取消原本的按钮

    // 初始化组件
    m_title = new Title(this);                   // 标题栏
    m_status = new Status(this);                 // 状态栏
    m_mainbar = new Mainbar(this);               // 登录界面,默认界面
    m_adminloginform = new adminLoginForm(m_mainbar,this); // admin登录界面
    m_userloginform = new userLoginForm(m_mainbar, this);   // user登录界面
    m_registerform = new registerForm(this);     // 注册界面
    m_findpassword = new findPasswordForm(this); // 找回密码界面
    m_uartframe = new uartframe(this);           // 串口界面

    // 配置堆叠布局
    m_stackedLayout = new QStackedLayout(ui->lamianbar); //ui->lamianbar作为m_stackedLayout的父对象
    m_stackedLayout->addWidget(m_mainbar);         // 索引0，默认显示
    m_stackedLayout->addWidget(m_adminloginform);  // 索引1
    m_stackedLayout->addWidget(m_userloginform);   // 索引2
    m_stackedLayout->addWidget(m_registerform);    // 索引3
    m_stackedLayout->addWidget(m_findpassword);    // 索引4
    m_stackedLayout->addWidget(m_uartframe);       // 索引5

    // 添加组件到布局
    ui->latitle->addWidget(m_title);
    ui->lastatus->addWidget(m_status);

    // mainbar信号槽连接
    connect(m_mainbar, &Mainbar::sendMessage, m_status, &Status::onReceiveMessage);
    connect(m_mainbar, &Mainbar::display, this, &Widget::onDisplayChanged);

    // m_registerform信号槽连接
    connect(m_registerform, &registerForm::sendMessage, m_status, &Status::onReceiveMessage);
    connect(m_registerform, &registerForm::display, m_stackedLayout, &QStackedLayout::setCurrentIndex);

    // m_findpassword信号槽连接
    connect(m_findpassword, &findPasswordForm::sendMessage, m_status, &Status::onReceiveMessage);
    connect(m_findpassword, &findPasswordForm::display, m_stackedLayout, &QStackedLayout::setCurrentIndex);

    // m_adminloginform信号槽连接
    connect(m_adminloginform, &adminLoginForm::sendMessage, m_status, &Status::onReceiveMessage);
    connect(m_adminloginform, &adminLoginForm::display, m_stackedLayout, &QStackedLayout::setCurrentIndex);

    // m_userloginform信号槽连接
    connect(m_userloginform, &userLoginForm::sendMessage, m_status, &Status::onReceiveMessage);
    connect(m_userloginform, &userLoginForm::display, this, &Widget::onDisplayChanged);

    // m_adminloginform信号槽连接
    connect(m_adminloginform, &adminLoginForm::sendMessage, m_status, &Status::onReceiveMessage);
    connect(m_adminloginform, &adminLoginForm::display, this, &Widget::onDisplayChanged);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::onDisplayChanged(int index)
{
    m_stackedLayout->setCurrentIndex(index);
}
